package sse.ustc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class SearchMusicIndexThread implements Runnable {
	
	private String name;
	public SearchMusicIndexThread(String name){
		this.name = name;
	}

	public Map<String, String> searchMusicLists() {
		Map<String, String> returnPath = new HashMap<String, String>();
		File file = new File("C:\\test");
		String[] files = file.list();
		java.util.Arrays.sort(files);
		String latestFile = files[files.length - 1];
		try {
			FileReader fileReader = new FileReader("C:\\test\\" + latestFile);
			BufferedReader bufReader = new BufferedReader(fileReader);
			String line = null;
			int i = 0;
			Map<String, String> maps = new HashMap<String, String>();
			String key, value;
			key = value = null;
			while ((line = bufReader.readLine()) != null) {
				i++;
				if (i % 2 == 0) {
					value = line;
					maps.put(key, value);
					key = value = null;
				} else {
					key = line;
				}
			}
			Object[] s = maps.keySet().toArray();
			System.out.println(s.length);
			for (int ii = 0; ii < s.length; ii++) {
				StringBuffer stringBuffer = new StringBuffer();
				stringBuffer.append(s[ii].toString().trim());
				for (int j = 0; j < stringBuffer.length(); j++) {
					int k = j + name.length();
					if (k > stringBuffer.length()) {
						break;
					}
					if (stringBuffer.subSequence(j, k).toString().equals(name)) {
						returnPath.put(s[ii].toString(), maps.get(
								s[ii].toString()).toString());
						break;
					}
				}

			}
		}// try
		catch (IOException e) {
			e.printStackTrace();
		}
		return returnPath;
	}

	@Override
	public void run() {
		Map<String,String> maps = this.searchMusicLists();
		System.out.println(maps.size());
		Object[] objs = maps.entrySet().toArray();
		for(Object o:objs){
			System.out.println(o);
		}
	}

	public static void main(String[] args) {
		Runnable searchMusicIndexThread = new SearchMusicIndexThread("s");
		Thread t = new Thread(searchMusicIndexThread);
		t.start();
	}
}
